//
//  ZGLIntelligencTeamCell.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/10/26.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLIntelligencTeamCell.h"
#import "ZGLContentHeaderView.h"
#import "ZGLIntelligenceContentCell.h"

static NSString *ZGLItelligenceTeamCellHeaderID = @"ZGLItelligenceTeamCellHeaderID";
@interface ZGLIntelligencTeamCell ()
<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *TeamLogo;
@property (weak, nonatomic) IBOutlet UILabel *TeamName;
@property (weak, nonatomic) IBOutlet UILabel *TeamLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightAuto;

@property (weak, nonatomic) IBOutlet UITableView *interlligenceContentListView;

@property (nonatomic, strong) ZGLContentHeaderView *tableHeaderView;

@property (nonatomic, strong) ZGLIntelligenceContentCell *contentCell;

@property (nonatomic, strong) NSArray <ZGLMessageModel *> *contentModels;

@end

@implementation ZGLIntelligencTeamCell

+ (instancetype)intelligenceTeamCell
{
    return [[[NSBundle mainBundle] loadNibNamed:@"ZGLIntelligencTeamCell" owner:nil options:nil]lastObject];;
}

- (ZGLIntelligenceContentCell *)contentCell
{
    if(!_contentCell){
        
        _contentCell = [ZGLIntelligenceContentCell XibIntelligenceContentCell];
    }
    return _contentCell;
}

- (NSArray<ZGLMessageModel *> *)contentModels
{
    if (!_contentModels) {
        _contentModels = [NSArray array];
    }
    return _contentModels;
}

- (void)setTeamDict:(NSDictionary *)teamDict
{
    _teamDict = teamDict;
    NSArray *arr = [teamDict allKeys];
    CGFloat  commentHeight = 0; //内容总高度
    CGFloat sections = 0;
    for (int i = 0; i<arr.count; i++) {
        NSArray *array = [teamDict objectForKey:arr[i]];
        if (array.count>0) {
            sections += 1;
        }
        for (NSDictionary *dict in array) {
            
            CGFloat height = [self heightForString:dict[@"content"] fontSize:13 andWidth:UI_SCREEN_WIDTH-52]+16;
            commentHeight += height;
        }
    }
    
    //判断组数
    CGFloat sectionH = 0;
    if (sections == 1) {
        sectionH = 60;
    }else if (sections == 2){
        sectionH = 110;
    }else if (sections == 3){
        sectionH = 160;
    }
    
    //设置内部tablewview高度
    self.tableViewHeightAuto.constant = commentHeight + sectionH;
//    self.tableViewHeightAuto.constant = self.interlligenceContentListView.contentSize.height+100;
    [self.interlligenceContentListView reloadData];
    [self.contentView updateConstraintsIfNeeded];
    
}

//用label模拟算高
- (float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width{
    
    CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    return sizeToFit.height;
}


- (void)setTeamIcon:(NSString *)teamIcon
{
    _teamIcon = teamIcon ;
    NSString *imageHeader = @"http://betcat.zeeging.com";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",imageHeader,_teamIcon];
    [self.TeamLogo sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"teamLogo_default"]];
    
}

- (void)setTeamNameLabel:(NSString *)teamNameLabel
{
    _teamNameLabel = teamNameLabel;
    self.TeamName.text = teamNameLabel;
}

- (void)setTeam:(NSString *)team
{
    _team = team;
    self.TeamLabel.text = team;
}

- (void)setIsHome:(BOOL)isHome
{
    _isHome = isHome;
}



- (void)awakeFromNib {
    [super awakeFromNib];
    
    //注册组头
    [self.interlligenceContentListView registerNib:[UINib nibWithNibName:@"ZGLContentHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:ZGLItelligenceTeamCellHeaderID];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sections = 0;
    NSArray *arr = [self.teamDict allKeys];
    for (int i = 0; i<arr.count; i++) {
        NSArray *array = [_teamDict objectForKey:arr[i]];
        if (array.count > 0) {
            sections +=1;
        }
    }
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *contentModels = [NSArray array];
    if (section == 0) {
        contentModels = self.teamDict[@"base"];
    }else if (section == 1){
        contentModels = self.teamDict[@"data"];
    }else if (section == 2){
        contentModels = self.teamDict[@"lineup"];
    }
    
    DLog(@"第-%ld-组，rows--%ld",section,contentModels.count);
    
    return contentModels.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ContentCellID = @"IntelligenceContentCell";
    ZGLIntelligenceContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ContentCellID];
    if (!cell) {
        cell = [ZGLIntelligenceContentCell XibIntelligenceContentCell];
    }
    ZGLMessageModel *model = [self ContentWith:indexPath];
    NSString *contentStr = model.content;
    cell.content = contentStr;
    cell.isHome = self.isHome;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZGLMessageModel *model = [self ContentWith:indexPath];
    self.contentCell.content = model.content;
    CGFloat H = [self.contentCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
    return H;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    static NSString *ZGLItelligenceTeamCellHeaderID = @"ZGLItelligenceTeamCellHeaderID";
    ZGLContentHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ZGLItelligenceTeamCellHeaderID];
    headerView.isHome = self.isHome;
    if (section == 0) {
        headerView.typeString = @"基础面";
    }else if (section == 1){
        headerView.typeString = @"数据";
    }else if (section == 2){
        headerView.typeString = @"阵容";
    }
    
    //解决背景色问题
    UIView *bgColorView = [[UIView alloc]initWithFrame:headerView.bounds];
    bgColorView.backgroundColor = [UIColor whiteColor];
    headerView.backgroundView = bgColorView;
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

//解析model
- (ZGLMessageModel *)ContentWith:(NSIndexPath *)indexPath
{
    NSArray <ZGLMessageModel *>*contentModels = [NSArray array];
    if (indexPath.section == 0) {
        contentModels = self.teamDict[@"base"];
    }else if (indexPath.section == 1){
        contentModels = self.teamDict[@"data"];
    }else if (indexPath.section == 2){
        contentModels = self.teamDict[@"lineup"];
    }
    
    NSArray *array = [ZGLMessageModel mj_objectArrayWithKeyValuesArray:contentModels];
    ZGLMessageModel *model = array[indexPath.row];
    
    return model;
}

@end


